<?php
/**
 * [weliam] Copyright (c) 2016/3/23
 * goods.ctrl
 * ��Ʒ���������
 */
defined('IN_IA') or exit('Access Denied');
wl_load()->model('goods');
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
global $_W,$_GPC;
//随机 猜你喜欢
if($op=='goods_rand'){
	$id=intval($_GPC['id']);
	$goods=pdo_fetch('select selltype from '.tablename('tg_goods').'where id=:id',array(':id'=>$id));
	$list=pdo_fetchall('select id,gname,gprice,share_image,gimg from '.tablename('tg_goods').' where uniacid=:uniacid and isshow=1 order by rand() limit 9',array(':uniacid'=>$_W['uniacid']));
	foreach ($list as $key => $value) {
		$list[$key]['url'] =app_url('goods/detail',array('id'=>$value['id']));
		if(!empty($value['share_image']))
		{
			$list[$key]['gimg']= tomedia($value['share_image']);
		}
		else{
			$list[$key]['gimg']= tomedia($value['gimg']);
		}
	}
	$data = array();
	$data['list'] = $list;
	$data['status'] = false;
	if($config['base']['guess']==1)
	{
		$data['status'] = true;
	}
	
	
	die(json_encode($data));
	//echo json_encode($list);
}
//商城提交页优惠券AJAX
if($op=='couponajax'){
	wl_load()->model('activity');
	$coupon = coupon_canuse($_W['openid'], $_GPC['productprice']);
	echo json_encode($coupon);
	exit;
}
if($op=='collect')
{
	$list=pdo_fetchall('select * from '.tablename('tg_collect').' where uniacid=:uniacid and openid=:openid and orderno=:orderno',array(':uniacid'=>$_W['uniacid'],':openid'=>$_W['openid'],':orderno'=>0));
	foreach ($list as $key => $value) {
		$goods=pdo_fetch('select * from '.tablename('tg_goods').'where id =:id',array(':id'=>$value['sid']));		
		$list[$key]['img']= tomedia($goods['gimg']);
		$list[$key]['price']= $value['oprice'];
		$list[$key]['title']= $goods['gname'];
		$list[$key]['weight']= $goods['weight'];
		$list[$key]['onelimit']= $goods['one_limit'];
		$list[$key]['manylimit']= $goods['many_limit'];
		$old_data=pdo_fetch("select * from ".tablename('tg_goods_openid').' where uniacid=:uniacid and openid=:openid and g_id=:g_id',array(':uniacid'=>$_W['uniacid'],':openid'=>$_W['openid'],':g_id'=>$value['sid']));
							
		$list[$key]['history_limit'] =intval($old_data['num']);
		
		$list[$key]['num']= $value['num'];
		$list[$key]['id']= $goods['id'];
		$list[$key]['kucun']= $goods['gnum'];
	}
	
	die(json_encode($list));
}
//商品添加
if($op =='add'){
	$id=$_GPC['id'];
	$str=$_GPC['guige'];//规格 
	$weight=$_GPC['weight'];//规格
	$kunum1 = pdo_fetch("SELECT productprice,stock FROM " . tablename('tg_goods_option') . " WHERE   goodsid='{$id}' and title='{$str}'  ");
	$price=$kunum1['productprice'];
	$gnum=$kunum1['stock'];
			if (empty($id)) {
				echo 0;
				exit;
			}else{
				$sql = 'SELECT oprice,supprices,storeid,credit,commissiontype,commission FROM '.tablename('tg_goods').' WHERE id=:id and uniacid=:uniacid';
		$paramse = array(':id'=>$id, ':uniacid'=>$_W['uniacid']);
		$goods = pdo_fetch($sql, $paramse);
		if(empty($str))
	{
		$price=$goods['oprice'];
	}
				$data=array(
	            'openid' => $_W['openid'],
	            'uniacid'=>$_W['uniacid'],
				'num'=>1,
				'oprice'=>$price,
				'orderno'=>0,
				'applystatus'=>0,
				'optionid'=>0,
				'item'=>$str,
				'weight'=>$weight,
				'supprices'=>$goods['supprices'],
				'storeid'=>$goods['storeid'],
				'credit'=>$goods['credit'],
				'type'=>$goods['commissiontype'],
				'commission'=>$goods['commission'],
	            'sid'=>$id
	            );
				
				$tt = pdo_fetch("SELECT id,num FROM " . tablename('tg_collect') . " WHERE  uniacid = '{$_W['uniacid']}' and sid='{$id}'  and openid='{$_W['openid']}' and item='{$str}'  and orderno='0'");
				$kunum = pdo_fetch("SELECT gnum FROM " . tablename('tg_goods') . " WHERE  uniacid = '{$_W['uniacid']}' and id='{$id}'  ");
				if(intval($gnum)==0&&empty($str))
				{
					$gnum=$kunum['gnum'];
				}
				$num=$tt['num']+1;
				if($num>intval($gnum)){echo '-1';exit;}
				if(empty($tt)){
	            if (pdo_insert('tg_collect', $data)) {
	            	echo 1;
	            }else{
	            	    //
					
			echo 0;
						//
	            }
				}
				else
				{
					pdo_update('tg_collect',array('num'=>$num),array('id' =>$tt['id']));
					echo 1;
					exit;
				}
			}
}
if($op =='remove'){
	$id=$_GPC['id'];
	$str=$_GPC['guige'];//规格
	if (empty($id)) {
				echo 0;
				exit;
			}else{
				//
				$tua = pdo_fetch("SELECT num,id FROM " . tablename('tg_collect') . " WHERE  sid = {$id} and uniacid = '{$_W['uniacid']}' AND orderno = '0' and item='{$str}'  and openid='{$_W['openid']}'");				
				$num=$tua['num']-1;
				if($num>0)
				{
				
				if (pdo_update('tg_collect',array('num'=>$num),array('id' =>$tua['id']))) {
					echo 1;
					exit;
				}
				}
				else
				{					
					if (pdo_delete('tg_collect', array('id' =>$tua['id']))) {
					echo 1;
					exit;
					}
				}
			//
				
				
			}
}
//幻灯片
if($op=='banner')
{
	$advs = pdo_fetchall("select * from " . tablename('tg_adv') . " where enabled = 1 and uniacid = '{$_W['uniacid']}' order by displayorder DESC");
	foreach ($advs as &$adv) {
		$adv['thumb']=tomedia($adv['thumb']);
		if (substr($adv['link'], 0, 5) != 'http:') {
			$adv['link'] = "http://" . $adv['link'];
		}
	}
die(json_encode($advs));
}
//分类
if($op=='category')
{
	$category = pdo_fetchall("SELECT * FROM " . tablename('tg_category') . " WHERE uniacid = '{$_W['uniacid']}' and enabled=1 and parentid=0 ORDER BY parentid ASC, displayorder DESC");
	foreach ($category as &$adv) {
		$adv['thumb']=tomedia($adv['thumb']);
		$adv['smallthumb']=tomedia($adv['smallthumb']);
	}
	die(json_encode($category));
}
//商品详情
if($op=='goods_detail'){
$id=intval($_GPC['id']);
$goods = goods_get_by_params("id = {$id}");
//得到图集
	$advs = pdo_fetchall("select * from " . tablename('tg_goods_atlas') . " where g_id='{$id}'");
    foreach ($advs as &$adv) {
		$adv['thumb']=tomedia($adv['thumb']);
    	if (substr($adv['link'], 0, 5) != 'http:') {
            $adv['link'] = "http://" . $adv['link'];
        }
    }
	$goods['advs']=$advs;
	$params = pdo_fetchall("SELECT * FROM" . tablename('tg_goods_param') .  "WHERE goodsid = '{$id}' ");
	$goods['params']=$params;
die(json_encode($goods));
}
if($op=='storelist'){
	global $_GPC;
	$uniacid=$_GPC['uniacid'];
	$g_id=$_GPC['g_id'];

	$goods = pdo_fetch("select hexiao_id from".tablename('tg_goods')."where id = '{$g_id}' and uniacid='{$uniacid}'");

	if(!empty($goods['hexiao_id']))
	{
		$store_ids = explode(',',substr($goods['hexiao_id'],0,strlen($goods['hexiao_id'])-1));
		$storelist=array();
		foreach($store_ids as $key=> $value){
			$store=pdo_fetch('select * from '.tablename('tg_store').' where id=:id',array(':id'=>$value));
			$storelist[$key]=$store;
		}
		die(json_encode($storelist));
	}else{
		die(json_encode(array('status'=>0)));
	}
	die(json_encode(array('status'=>0)));
}
?>